home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wpjv1n2.zip / D&DSERV.PAS < prev    next >
Pascal/Delphi Source File  |  1993-01-31  |  5KB  |  196 lines

  1. (*****************************************)
  2. (*                                       *)
  3. (*   D&D-Server (d&dserv.pas)            *)
  4. (*                                       *)
  5. (*   A simple app to demonstrate the     *)
  6. (*   programming of a Drag&Drop-server   *)
  7. (*                                       *)
  8. (*   Copyright (c) 1992,93 by A. Furrer  *)
  9. (*                                       *)
  10. (*   This program requires Windows 3.1   *)
  11. (*****************************************)
  12.  
  13. program DDServ;
  14.  
  15. uses Strings, WinTypes, WinProcs, WObjects;
  16.  
  17. const ws_Ex_AcceptFiles = $00000010;
  18.       wm_DropFiles      = $0233;
  19.  
  20.  
  21. (* These files we want to drop to a D&D-Client *)
  22.  
  23. const s : array[1..3,0..255] of char=('C:\autoexec.bat',
  24.                                       'C:\config.sys',
  25.                                       'C:\dos\command.com');
  26.  
  27. (****************************************************)
  28. (* DragDropStruct                                   *)
  29. (*                                                  *)
  30. (* This is the Drag&Drop structure used by Windows. *)
  31. (* The format is NOT documented so it can change    *)
  32. (* in the next release of Wndows.                   *)
  33. (****************************************************)
  34.  
  35. type PDragDropStruct =^TDragDropStruct;
  36.      TDragDropStruct = record
  37.        DataOffset      : word;
  38.        DropPoint       : TPoint;
  39.        DropInNonClient : bool;
  40.        Data            : array[0..0] of char;
  41.      end;
  42.  
  43. (****************)
  44. (* Mainwindow   *)
  45. (****************)
  46.  
  47. type
  48.   PMainwindow = ^TMainwindow;
  49.   TMainwindow =object(TWindow)
  50.     Drag : boolean;
  51.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  52.     procedure WMLButtonDown(var Msg : TMessage); virtual wm_first +
  53.  wm_LButtonDown;
  54.     procedure WMMouseMove(var Msg : TMessage); virtual wm_first + wm_MouseMove;
  55.     procedure WMLButtonUp(var Msg : TMessage); virtual wm_first + wm_LButtonUp;
  56.   end;
  57.  
  58.  
  59. constructor TMainwindow.Init;
  60. begin
  61.   TWindow.Init(AParent,ATitle);
  62.   Drag:=false;
  63. end;
  64.  
  65. procedure TMainwindow.WMLButtonDown;
  66. begin
  67.   (* Now be begin to Drag&Drop        *)
  68.   (* and we have to capture the mouse *)
  69.   Drag:=true;
  70.   SetCapture(HWindow);
  71. end;
  72.  
  73. procedure TMainwindow.WMMouseMove;
  74. var DragWnd : HWnd;
  75.     Point : TPoint;
  76. begin
  77.   (* if we do Drag&Drop *)
  78.   if Drag then begin
  79.  
  80.     (* get the window under the cursor      *)
  81.     Point.X:=LoWord(GetMessagePos);
  82.     Point.Y:=HiWord(GetMessagePos);
  83.     DragWnd:=WindowFromPoint(Point);
  84.  
  85.     (* and test if it accepts dropped files *)
  86.     if GetWindowLong(DragWnd,gwl_ExStyle) and
  87.  ws_Ex_AcceptFiles=ws_Ex_AcceptFiles then
  88.  
  89.     (* set the cursor to a cross            *)
  90.       SetCursor(LoadCursor(0,idc_Cross))
  91.     else
  92.  
  93.     (* set the default cursor               *)
  94.       SetCursor(LoadCursor(0,idc_Arrow))
  95.   end;
  96. end;
  97.  
  98. procedure TMainwindow.WMLButtonUp;
  99. var DropWnd : HWnd;
  100.     Point : TPoint;
  101.     DataHandle : THandle;
  102.     DataPtr,p : PChar;
  103.     l : longint;
  104.     i : word;
  105. begin
  106.   (* if we do Drag&Drop *)
  107.   if Drag then begin
  108.  
  109.     (* get the window under the cursor  *)
  110.     Point.X:=LoWord(GetMessagePos);
  111.     Point.Y:=HiWord(GetMessagePos);
  112.     DropWnd:=WindowFromPoint(Point);
  113.  
  114.     (* and test if it accepts dropped files *)
  115.     if GetWindowLong(DropWnd,gwl_ExStyle) and
  116.  ws_Ex_AcceptFiles=ws_Ex_AcceptFiles then begin
  117.  
  118.       (* Count the length of the data   *)
  119.       l:=0;
  120.       for i:=1 TO 3 do
  121.         l:=l+StrLen(s[i])+1;
  122.  
  123.       (* and allocate memory            *)
  124.       DataHandle:=GlobalAlloc(gmem_DDEShare,sizeof(TDragDropStruct)+l);
  125.  
  126.       (* To set the data we have to     *)
  127.       (* lock the memory                *)
  128.       DataPtr:=GlobalLock(DataHandle);
  129.  
  130.       (* fill in the Drag&Drop structur *)
  131.       with PDragDropStruct(DataPtr)^ do begin
  132.         DataOffset:=Data-DataPtr;
  133.  
  134.  DropInNonClient:=(DefWindowProc(DropWnd,wm_NCHitTest,0,longint(Point))<>htClien
  135.  t);
  136.         ScreenToClient(DropWnd,Point);
  137.         DropPoint   :=Point;
  138.       end;
  139.  
  140.       (* now we set the data            *)
  141.       p:=PDragDropStruct(DataPtr)^.Data;
  142.       for i:=1 to 3 do begin
  143.         StrCopy(p,s[i]);
  144.         p:=p+StrLen(s[i])+1;
  145.       end;
  146.  
  147.       (* mark the end of the data       *)
  148.       p^:=#0;
  149.  
  150.       (* and unlock the memory          *)
  151.       GlobalUnlock(DataHandle);
  152.  
  153.  
  154.       (* post the wm_DropFiles message  *)
  155.       (* to the window under the cursor *)
  156.       PostMessage(DropWnd,wm_DropFiles,DataHandle,0);
  157.  
  158.       (* set the default cursor and     *)
  159.       (* release the capture            *)
  160.       SetCursor(LoadCursor(0,idc_Arrow));
  161.       ReleaseCapture;
  162.       Drag:=false;
  163.     end;
  164.   END;
  165. end;
  166.  
  167.  
  168. (**********************)
  169. (* TDDServApplication *)
  170. (**********************)
  171.  
  172. type
  173.    TDDServApplication =
  174.      object(TApplication)
  175.        procedure InitMainWindow; virtual;
  176.      end;
  177.  
  178. procedure TDDServApplication.InitMainWindow;
  179. begin
  180.   MainWindow := New(PMainwindow,Init(nil, 'D&DServer'));
  181. end;
  182.  
  183. (***************)
  184. (* Mainprogram *)
  185. (***************)
  186.  
  187. var
  188.    Prg : TDDServApplication;
  189. begin
  190.   Prg.Init('D&DServer');
  191.   Prg.Run;
  192.   Prg.Done;
  193. end.
  194.  
  195.  
  196.